/*
@Author   : Uyynot
@Email    : uyynot@qq.com
@Time     : 2023/8/25 13:23
@File     : retrieve.go
@Project  : web_scaff
@Desc     :
*/
package user

import (
	"time"
	"web_scaff/models/response"
	sysValidator "web_scaff/pkg/validator"
	"web_scaff/service/user"

	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"

	"github.com/gin-gonic/gin"
)

type retrieveRequest struct {
	Id string `eform:"id" uri:"id" binding:"required"`
}

type retrieveResponse struct {
	Uid       int64     `json:"uid"`
	Name      string    `json:"name"`
	Phone     string    `json:"phone"`
	Avatar    string    `json:"avatar"`
	Username  string    `json:"username"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
}

func Retrive(c *gin.Context) {
	var req = new(retrieveRequest)
	if err := c.ShouldBindUri(req); err != nil {
		// 请求参数有误，直接返回响应
		zap.L().Error("添加用户失败", zap.Error(err))
		// 判断err是不是validator.ValidationErrors 类型
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			response.FailWithMsg("查询用户失败", c)
			return
		}
		response.FailWithMsg(sysValidator.RemoveTopStruct(errs.Translate(sysValidator.Trans)), c)
		return
	}

	u, err := user.Retrive(req.Id)
	if err != nil {
		response.FailWithMsg(err.Error(), c)
		return
	}
	responseUser := retrieveResponse{
		Uid:       u.Uid,
		Name:      u.Name,
		Phone:     u.Phone,
		Avatar:    u.Avatar,
		Username:  u.Username,
		CreatedAt: u.CreatedAt,
		UpdatedAt: u.UpdatedAt,
	}
	response.Result(0, responseUser, "success", c)
}
